package com.hm.health.Utils;

import android.content.Context;
import android.os.Environment;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yxx on 7/15/16.
 */
public class UtilsPersistence {
    public static boolean saveArrayList(String dest, ArrayList arrayList, Context context) {
        boolean ret = true;
        try {
            Gson gson = new Gson();
            FileOutputStream outputStream = context.openFileOutput(dest, Context.MODE_PRIVATE);
            FileWriter fw = new FileWriter(outputStream.getFD());
            gson.toJson(arrayList, fw);
            fw.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            ret = false;
            e.printStackTrace();
        } catch (IOException e) {
            ret = false;
            e.printStackTrace();
        }

        return ret;
    }

    public static ArrayList extractArrayList(Type type, String source, Context context) {
        ArrayList list = null;
        try {
            Gson gson = new Gson();
            FileInputStream fileInputStream = context.openFileInput(source);
            FileReader fileReader = new FileReader(fileInputStream.getFD());
            list = gson.fromJson(fileReader, type);
            fileReader.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static boolean saveHashMap(String dest, HashMap<String, ArrayList> resultNameAndValue, Context context) {
        boolean ret = true;
        try {
            Gson gson = new Gson();
            FileOutputStream outputStream = context.openFileOutput(dest, Context.MODE_PRIVATE);
            FileWriter fw = new FileWriter(outputStream.getFD());
            gson.toJson(resultNameAndValue, fw);
            String info = gson.toJson(resultNameAndValue);
            fw.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            ret = false;
            e.printStackTrace();
        } catch (IOException e) {
            ret = false;
            e.printStackTrace();
        }

        return ret;
    }

    public static HashMap<String, ArrayList> extractHashMap(Type type, String source, Context context) {
        HashMap<String, ArrayList> list = null;
        try {
            Gson gson = new Gson();
            FileInputStream fileInputStream = context.openFileInput(source);
            FileReader fileReader = new FileReader(fileInputStream.getFD());
            list = gson.fromJson(fileReader, type);
            fileReader.close();
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 保存16进制数据到文件
     * @param strPath
     * @param dest
     * @param arrData
     * @return
     */
    public static boolean saveAsTxt(String strPath, String dest, byte[] arrData) {
        boolean ret = true;

        if ((null == strPath) || (null == dest)) {
            return false;
        }

        //save in sdcard
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File filePath = new File(strPath);

            if(!filePath.exists())
            {
                filePath.mkdirs();
            }

            File sdFile = new File(filePath, dest + ".txt");

            try {
                FileOutputStream fos = new FileOutputStream(sdFile, true);
                fos.write(HexUtil.encodeHexStr(arrData).getBytes());
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                ret = false;
            } catch (IOException e) {
                e.printStackTrace();
                ret = false;
            }
        }
        return ret;
    }

    /**
     * 保存心率数据到文件
     * @param strPath
     * @param dest
     * @param listHeartRate
     * @return
     */
    public static boolean saveHRDataToCsv(String strPath, String dest, List<Integer> listHeartRate) {
        boolean ret = true;

        if ((null == strPath) || (null == dest)) {
            return false;
        }

        //save in sdcard
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            File filePath = new File(strPath);

            if(!filePath.exists())
            {
                filePath.mkdirs();
            }

            File sdFile = new File(filePath, dest + ".csv");

            try {
                FileOutputStream fos = new FileOutputStream(sdFile, true);
                for (Integer val : listHeartRate) {
                    String strTmp = String.format("%d \n", val);
                    fos.write(strTmp.getBytes());
                }
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                ret = false;
            } catch (IOException e) {
                e.printStackTrace();
                ret = false;
            }
        }

        return ret;
    }

    /**
     * Save tester information
     * @param strPath
     * @param dest
     * @param strTesterInfo
     * @param context
     * @return
     */
    public static boolean saveTeseterInfoAsCsv(String strPath, String dest, String strTesterInfo, Context context) {

        boolean ret = true;

        if ((null == strPath) || (null == dest)) {
            return false;
        }

        //save in sdcard
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

            File filePath = new File(strPath);

            if(!filePath.exists())
            {
                filePath.mkdirs();
            }

            File sdFile = new File(filePath, dest + ".header");

            try {
                FileOutputStream fos = new FileOutputStream(sdFile);
                byte[] buf = strTesterInfo.getBytes();
                fos.write(buf);
                fos.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                ret = false;
            } catch (IOException e) {
                e.printStackTrace();
                ret = false;
            }
        }

        return ret;
    }
}
